package cate.game.activity.rank;

import cate.common.table.activity.rankactivity.row.RankActivityBaseRow;
import cate.common.table.d.GDOperation;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.crossrank.po.CrossRankActivity;
import cate.game.activity.rank.msg.RankActivityDataResp;
import cate.game.mail.po.MailBuilder;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.history.HistoryItem;
import cate.game.role.mail.Mail;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

public class RoleRankActivity extends RoleActivitySingle {
    @NoteField("排行榜的值")
    public long rankValue;
    @NoteField("是否被记录过")
    public boolean recorded;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        role.getActivity().registerRecharge(this, this::onPay);
    }

    @Override
    public void onEnter(){
        super.onEnter();
        if (!recorded) {
            triggerEnterRecord();
        }
    }

    protected void onPay(long payCents) {
        SingleRankActivityHandler activityHandler = role.getGame().activityGlobal.getEnableHandler(funcId);
        if (activityHandler != null) {
            activityHandler.updateExtraValue(role.getUid(), payCents / 100, true);
        }
    }

    @Override
    public void excelCheck() {

    }

    @Override
    public void onRoleOpen() {
        rankValue = 0;
        recorded = false;
        triggerEnterRecord();
    }

    @Override
    public void onRoleEnd() {
        if (rankValue > 0) {
            RankActivityBaseRow baseRow = getBaseRow();
            if (baseRow != null && baseRow.backMailId > 0 && baseRow.backScale > 0 && StringUtils.isNotBlank(baseRow.backRewardStr)) {
                int backScale = (int) (rankValue * baseRow.backScale);
                if (backScale > 0) {
                    Mail mail = new MailBuilder()
                            .setReward(new MixRes(baseRow.backRewardStr).multiply(backScale))
                            .setContentId(baseRow.backMailId)
                            .setOperation(GDOperation.RANK_ACTIVITY_BACK_REWARD)
                            .build();
                    role.getMail().add(mail);
                }
            }
        }
        rankValue = 0;
        recorded = false;
    }

    @Override
    public void initSend() {
        role.sendNow(new RankActivityDataResp(this));
    }

    @JsonIgnore
    protected RankActivityBaseRow getBaseRow() {
        return role.getGame().table.rankActivity.base.get(configTid());
    }

    /**
     * 触发上线记录
     */
    private void triggerEnterRecord() {
        recorded = true;
        RankActivityBaseRow baseRow = getBaseRow();
        if (baseRow != null && baseRow.readHistory) {
            HistoryItem historyItem = role.getHistory().getItem(baseRow.event, baseRow.param);
            if (historyItem != null && historyItem.cur > 0 && !(this instanceof CrossRankActivity)) {
                SingleRankActivityHandler activityHandler = role.getGame().activityGlobal.getEnableHandler(funcId);
                activityHandler.doRecord(role, baseRow.event, baseRow.param, historyItem.cur, true);
            }
        }
    }

    public long record(long newValue, boolean add){
        recorded = true;
        if (add) {
            this.rankValue += newValue;
        } else {
            if (this.rankValue < newValue) {
                this.rankValue = newValue;
            }
        }
        return this.rankValue;
    }
}
